<?php

namespace app\admin\service;

use app\admin\model\BoxDataOverview;
use app\admin\model\BoxFacilityRemainLog;

class BoxReportFacilityRemainService extends BaseService
{

    /**
     * @Notes:设备留存
     * @Interface getList
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }

        if (isset($param['platform']) && !empty($param['platform'])) {
            $map[] = ['platform', '=', $param['platform']];
        }

        $list = (new BoxFacilityRemainLog())
            ->field('day_time, sum(device_num) as device_num, sum(d2) as d2, sum(d3) as d3, sum(d4) as d4, sum(d5) as d5, sum(d6) as d6, sum(d7) as d7, sum(d14) as d14, sum(d30) as d30, sum(d45) as d45, sum(d60) as d60, sum(d90) as d90, sum(d120) as d120, sum(d180) as d180, sum(d360) as d360')
            ->where($map)
            ->order('day_time', 'desc');

        if (empty($param['platform'])) {
            $list->group('day_time');
        } else {
            $list->group('day_time,platform');
        }

        $list = $list->select()->toArray();
        //获取数据总数
        $count = count($list);

        if (!empty($list)) {
            $device_num_all = 0;
            $d2_all         = 0;
            $d3_all         = 0;
            $d4_all         = 0;
            $d5_all         = 0;
            $d6_all         = 0;
            $d7_all         = 0;
            $d14_all        = 0;
            $d30_all        = 0;
            $d45_all        = 0;
            $d60_all        = 0;
            $d90_all        = 0;
            $d120_all       = 0;
            $d180_all       = 0;
            $d360_all       = 0;
            foreach ($list as $key => &$item) {
                $device_num_all += $item['device_num'];
                if (!empty($item['device_num'])) {
                    $d2_all   += round($item['d2'] / $item['device_num'] * 100);
                    $d3_all   += round($item['d3'] / $item['device_num'] * 100);
                    $d4_all   += round($item['d4'] / $item['device_num'] * 100);
                    $d5_all   += round($item['d5'] / $item['device_num'] * 100);
                    $d6_all   += round($item['d6'] / $item['device_num'] * 100);
                    $d7_all   += round($item['d7'] / $item['device_num'] * 100);
                    $d14_all  += round($item['d14'] / $item['device_num'] * 100);
                    $d30_all  += round($item['d30'] / $item['device_num'] * 100);
                    $d45_all  += round($item['d45'] / $item['device_num'] * 100);
                    $d60_all  += round($item['d60'] / $item['device_num'] * 100);
                    $d90_all  += round($item['d90'] / $item['device_num'] * 100);
                    $d120_all += round($item['d120'] / $item['device_num'] * 100);
                    $d180_all += round($item['d180'] / $item['device_num'] * 100);
                    $d360_all += round($item['d360'] / $item['device_num'] * 100);
                }

                $item['day_time'] = date("Y-m-d", strtotime($item['day_time']));//日期
                $item['d2']       = empty($item['device_num']) ? "0%" : round($item['d2'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d3']       = empty($item['device_num']) ? "0%" : round($item['d3'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d4']       = empty($item['device_num']) ? "0%" : round($item['d4'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d5']       = empty($item['device_num']) ? "0%" : round($item['d5'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d6']       = empty($item['device_num']) ? "0%" : round($item['d6'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d7']       = empty($item['device_num']) ? "0%" : round($item['d7'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d14']      = empty($item['device_num']) ? "0%" : round($item['d14'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d30']      = empty($item['device_num']) ? "0%" : round($item['d30'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d45']      = empty($item['device_num']) ? "0%" : round($item['d45'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d60']      = empty($item['device_num']) ? "0%" : round($item['d60'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d90']      = empty($item['device_num']) ? "0%" : round($item['d90'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d120']     = empty($item['device_num']) ? "0%" : round($item['d120'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d180']     = empty($item['device_num']) ? "0%" : round($item['d180'] / $item['device_num'] * 100) . "%";//2日留存
                $item['d360']     = empty($item['device_num']) ? "0%" : round($item['d360'] / $item['device_num'] * 100) . "%";//2日留存

            }
            $list[$count]['day_time']   = "汇总";
            $list[$count]['device_num'] = $device_num_all;
            $list[$count]['d2']         = round($d2_all / $count) . "%";
            $list[$count]['d3']         = round($d3_all / $count) . "%";
            $list[$count]['d4']         = round($d4_all / $count) . "%";
            $list[$count]['d5']         = round($d5_all / $count) . "%";
            $list[$count]['d6']         = round($d6_all / $count) . "%";
            $list[$count]['d7']         = round($d7_all / $count) . "%";
            $list[$count]['d14']        = round($d14_all / $count) . "%";
            $list[$count]['d30']        = round($d30_all / $count) . "%";
            $list[$count]['d45']        = round($d45_all / $count) . "%";
            $list[$count]['d60']        = round($d60_all / $count) . "%";
            $list[$count]['d90']        = round($d90_all / $count) . "%";
            $list[$count]['d120']       = round($d120_all / $count) . "%";
            $list[$count]['d180']       = round($d180_all / $count) . "%";
            $list[$count]['d360']       = round($d360_all / $count) . "%";
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list,
            "count" => $count,
        ];
    }

}
